﻿#region

using System;
using System.Collections.Generic;
using MediaSage.Lib.Utilities;
using NHibernate;
using NHibernate.Criterion;

#endregion

namespace MediaSage.Lib.Database
{
    public class EpisodeEntity : DatabaseObject
    {
        #region Properties

        public virtual int AbsoluteNumber { get; set; }

        public virtual int AirsAfterSeason { get; set; }

        public virtual int AirsBeforeSeason { get; set; }

        public virtual ImageEntity Banner { get; set; }

        public virtual ImageEntity SeasonBanner { get; set; }

        public virtual IList<PersonEntity> Cast { get; set; }

        public virtual int DvdChapter { get; set; }

        public virtual int DvdDiscID { get; set; }

        public virtual double DvdEpisodeNumber { get; set; }

        public virtual int DvdSeason { get; set; }

        public virtual string EpisodeName { get; set; }

        public virtual int EpisodeNumber { get; set; }

        public virtual DateTime? FirstAired { get; set; }

        public virtual Guid ID { get; protected set; }

        public virtual string IMDBID { get; set; }

        public virtual bool IsSpecial { get; set; }

        public virtual string Language { get; set; }

        public virtual string Overview { get; set; }

        public virtual string ProductionCode { get; set; }

        public virtual double Rating { get; set; }

        public virtual int SeasonID { get; set; }

        public virtual int SeasonNumber { get; set; }

        public virtual int SeriesID { get; set; }

        public virtual int TvdbID { get; set; }

        #endregion Properties

        public EpisodeEntity()
        {
            Cast = new List<PersonEntity>();
        }

        public override void Save()
        {
            ISession session = DBUtil.GetSession();
            using (session.BeginTransaction())
            {
                var dbCE =
                    session.CreateCriteria(typeof(EpisodeEntity)).Add(
                        Restrictions.Or(Restrictions.Eq("SeriesID", SeriesID), Restrictions.Eq("TvdbID", TvdbID))).
                        UniqueResult<EpisodeEntity>();
                if (dbCE != null)
                {
                    EpisodeEntity mEE = MediaUtil.MergeEpisodeEntity(dbCE, this, config.MergeType,
                                                                     config.MergeOnPersonName);
                    AbsoluteNumber = mEE.AbsoluteNumber;
                    AirsAfterSeason = mEE.AirsAfterSeason;
                    AirsBeforeSeason = mEE.AirsBeforeSeason;
                    Banner = mEE.Banner;
                    SeasonBanner = mEE.SeasonBanner;
                    Cast = mEE.Cast;
                    DvdChapter = mEE.DvdChapter;
                    DvdDiscID = mEE.DvdDiscID;
                    DvdEpisodeNumber = mEE.DvdEpisodeNumber;
                    DvdSeason = mEE.DvdSeason;
                    EpisodeName = mEE.EpisodeName;
                    EpisodeNumber = mEE.EpisodeNumber;
                    FirstAired = mEE.FirstAired;
                    ID = dbCE.ID;
                    IMDBID = mEE.IMDBID;
                    IsSpecial = mEE.IsSpecial;
                    Language = mEE.Language;
                    Overview = mEE.Overview;
                    ProductionCode = mEE.ProductionCode;
                    Rating = mEE.Rating;
                    SeasonID = mEE.SeasonID;
                    SeasonNumber = mEE.SeasonNumber;
                    SeriesID = mEE.SeriesID;
                    TvdbID = mEE.TvdbID;
                }
            }
            base.Save();
        }

        public override void SaveOrUpdate()
        {
            ISession session = DBUtil.GetSession();
            using (session.BeginTransaction())
            {
                var dbCE =
                    session.CreateCriteria(typeof(EpisodeEntity)).Add(
                        Restrictions.Or(Restrictions.Eq("SeriesID", SeriesID), Restrictions.Eq("TvdbID", TvdbID))).
                        UniqueResult<EpisodeEntity>();
                if (dbCE != null)
                {
                    EpisodeEntity mEE = MediaUtil.MergeEpisodeEntity(dbCE, this, config.MergeType,
                                                                     config.MergeOnPersonName);
                    AbsoluteNumber = mEE.AbsoluteNumber;
                    AirsAfterSeason = mEE.AirsAfterSeason;
                    AirsBeforeSeason = mEE.AirsBeforeSeason;
                    Banner = mEE.Banner;
                    SeasonBanner = mEE.SeasonBanner;
                    Cast = mEE.Cast;
                    DvdChapter = mEE.DvdChapter;
                    DvdDiscID = mEE.DvdDiscID;
                    DvdEpisodeNumber = mEE.DvdEpisodeNumber;
                    DvdSeason = mEE.DvdSeason;
                    EpisodeName = mEE.EpisodeName;
                    EpisodeNumber = mEE.EpisodeNumber;
                    FirstAired = mEE.FirstAired;
                    ID = dbCE.ID;
                    IMDBID = mEE.IMDBID;
                    IsSpecial = mEE.IsSpecial;
                    Language = mEE.Language;
                    Overview = mEE.Overview;
                    ProductionCode = mEE.ProductionCode;
                    Rating = mEE.Rating;
                    SeasonID = mEE.SeasonID;
                    SeasonNumber = mEE.SeasonNumber;
                    SeriesID = mEE.SeriesID;
                    TvdbID = mEE.TvdbID;
                }
            }
            base.Save();
        }
    }
}